import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:liuxlcommon/view/my_box.dart';

typedef ValueGetter1 = String Function(int position);

class CarouselView1 extends StatefulWidget {
  ValueGetter1 getItemPath;

  List datas;
  ValueChanged onClick;

  CarouselView1(this.datas, this.getItemPath, this.onClick);

  @override
  _CarouselState createState() => _CarouselState();
}

class _CarouselState extends State<CarouselView1> {
  int _pageIndex = 10;
  late PageController _pageController;
    Timer? timer;

  @override
  void dispose() {
    super.dispose();
    timer?.cancel();
  }

  @override
  void initState() {
    super.initState();
    Timer.periodic(Duration(seconds: 3), (timer) { //callback function
      try{
        int i=_pageIndex+1;
        _pageController.animateToPage(i,
            duration: const Duration(milliseconds: 500), curve: Curves.ease);
      }catch(e){
        print(e);
      }
    });
    _pageController = PageController(
      initialPage: 10,
      viewportFraction: 0.90,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Column(children: <Widget>[
      SizedBox(
        height: 160.w,
        child: PageView.builder(
          pageSnapping: true,
          itemCount: 90000,
          controller: _pageController,
          onPageChanged: (int index) {
            setState(() {
              _pageIndex = index;
            });
          },
          itemBuilder: (BuildContext context, int index) {
            return _buildItem(_pageIndex, index);
          },
        ),
      ),
      10.lsh,
      Container(
          width: Get.width,
          child: Center(
              child: PageIndicator(_pageIndex, widget.datas.length)))
    ]);
  }

  Widget _buildItem(activeIndex, index) {
    int po = widget.datas.length > index ? index : index % widget.datas.length;
    return Container(
        margin: EdgeInsets.symmetric(horizontal: 5.w),
        child: InkWell(
          child: LImage.easy(
              widget.getItemPath(po), double.infinity, double.infinity, 6.r),
          onTap: () => widget.onClick(po),
        ));
  }
}

class PageIndicator extends StatelessWidget {
  final int currentIndex;
  final int pageCount;

  PageIndicator(this.currentIndex, this.pageCount);

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: _buildIndicators(),
    );
  }

  Widget _indicator(bool isActive) {
    return Container(
      width: 6.0,
      height: 6.0,
      margin: EdgeInsets.symmetric(horizontal: 3.0),
      decoration: BoxDecoration(
        color: isActive ? Color(0xff666a84) : Color(0xffb9bcca),
        shape: BoxShape.circle,
        boxShadow: [
          BoxShadow(
            color: Colors.black12,
            offset: Offset(0.0, 3.0),
            blurRadius: 3.0,
          ),
        ],
      ),
    );
  }

  List<Widget> _buildIndicators() {
    List<Widget> indicators = [];
    int po = pageCount > currentIndex ? currentIndex : currentIndex % pageCount;
    for (int i = 0; i < pageCount; i++) {
      indicators.add(i == po ? _indicator(true) : _indicator(false));
    }
    return indicators;
  }
}